<%@ page import="java.awt.image.BufferedImage" %>
<%@ page import="java.awt.*" %>
<%@ page import="java.io.OutputStream" %>
<%@ page import="javax.imageio.ImageIO" %>
<%@ page contentType="image/jpeg" language="java" %>
<%
    // 从Session中获取验证码文本
    String captcha = (String) session.getAttribute("captcha");
    java.util.Random random = new java.util.Random();
    // 生成验证码图片
    int width = 200;
    int height = 50;
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = image.createGraphics();

    // 设置背景色
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, width, height);

    // 设置字体和颜色
    g.setFont(new Font("Arial", Font.BOLD, 30));
    g.setColor(Color.BLACK);

    // 绘制验证码文本
    g.drawString(captcha, 20, 40);

    // 添加干扰线
    for (int i = 0; i < 5; i++) {
        g.setColor(new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
        g.drawLine(random.nextInt(width), random.nextInt(height), random.nextInt(width), random.nextInt(height));
    }

    g.dispose();

    // 输出图像
    OutputStream out1 = response.getOutputStream();
    ImageIO.write(image, "jpeg", out1);
    out1.close();
%>